/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.fatecpg.songs.dao.hibernate;

import br.com.fatecpg.songs.entity.Album;
import org.hibernate.Criteria;
import org.hibernate.criterion.Criterion;
import static org.hibernate.criterion.MatchMode.ANYWHERE;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Eloyza
 */

/* Consulta de álbum por titulo,gravadora, nome do artista ou nome de música */
public class AlbumDAO extends SearchableDAO<Album> implements br.com.fatecpg.songs.dao.AlbumDAO {

    public AlbumDAO() {
        super(Album.class);
    }
    
    

    @Override
    public void setAlias(Criteria c) {
        c.createAlias("recorder", "r", Criteria.LEFT_JOIN)
         .createAlias("artist", "a", Criteria.LEFT_JOIN)
         .createAlias("songs", "s", Criteria.LEFT_JOIN);
    }

    @Override
    public void setOrder(Criteria c) {
        c.addOrder(Order.asc("title"))
         .addOrder(Order.asc("year"));
    }

    @Override
    public Criterion getSearchCriterion(String searchedWord) {
        Criterion criterion = Restrictions.or(
                Restrictions.ilike("title", searchedWord, ANYWHERE),
                Restrictions.or(
                        Restrictions.ilike("r.name", searchedWord, ANYWHERE),
                        Restrictions.or(
                                Restrictions.ilike("a.name", searchedWord, ANYWHERE),
                                Restrictions.ilike("s.title", searchedWord, ANYWHERE)
                        )
                )
        );
        return criterion;
    }
}
